package com.spn.blog.common.utils;


import io.jsonwebtoken.Jwt;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.codec.digest.DigestUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class JWTUtils {

    private static final String jwtToken = "123456spn!@#$$";
    public static final String userKey = "userId";

    public static String createToken(String userId){
        Map<String,Object> claims = new HashMap<>();
        claims.put(userKey,userId);
        JwtBuilder jwtBuilder = Jwts.builder()
                //A 签发算法，秘钥为jwtToken
                .signWith(SignatureAlgorithm.HS256, jwtToken)
                //B body数据，要唯一，自行设置
                .setClaims(claims)
                // 设置签发时间
                .setIssuedAt(new Date())
                // 一天的有效时间
                .setExpiration(new Date(System.currentTimeMillis() + 24 * 60 * 60 * 60 * 1000));
        String token = jwtBuilder.compact();
        return token;
    }

    public static Map<String, Object> checkToken(String token){
        try {
            Jwt parse = Jwts.parser().setSigningKey(jwtToken).parse(token);
            return (Map<String, Object>) parse.getBody();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;

    }


    public static void main(String[] args) {
        String s = DigestUtils.md5Hex("admin" + "spn!@#");
        System.out.println(s);
        /*String token = JWTUtils.createToken(1L);
        System.out.println(token);
        Map<String, Object> stringObjectMap = JWTUtils.checkToken(token);

        System.out.println(stringObjectMap.get("userId"));*/
    }
    /*public static void main(String[] args) {
        BasicTextEncryptor textEncryptor = new BasicTextEncryptor();
        //加密所需的salt
        textEncryptor.setPassword("mszlu_blog_$#@wzb_&^%$#");
        //要加密的数据（数据库的用户名或密码）
        String username = textEncryptor.encrypt("root");
        String password = textEncryptor.encrypt("root");
        System.out.println("username:"+username);
        System.out.println("password:"+password);
        System.out.println(textEncryptor.decrypt("29cZ+X9cNmECjbLXT2P/BBZWReVl30NS"));
    }*/

}
